package com.qianbao.qianbaobusiness.ui.push;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidbase.utils.StringUtils;
import com.androidbase.webview.WebViewTbsActivity;
import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseLceMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MessageListBean;
import com.qianbao.qianbaobusiness.ui.push.adapter.HomeMessageAdapter;
import com.qianbao.qianbaobusiness.ui.webview.SimpleWebViewActivity;

import butterknife.BindView;

public class MessageListActivity extends BaseLceMvpActivity<MessageView, MessageRxPresent> implements SwipeRefreshLayout.OnRefreshListener, BaseRecycleViewAdapter.OnLoadMoreListener, BaseRecycleViewAdapter.OnItemClickListener, MessageView, HomeMessageAdapter.OnItemClickListener, View.OnClickListener {

    public final static int MESSAGE_MIN_NUM = 12;
    public final static int PAGE_NUM_PRE = 20;

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbar_back;
    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.right_text)
    TextView right_text;
    @BindView(R.id.rcv_list_id)
    RecyclerView rcv_list_id;
    @BindView(R.id.ptr_fresh_id)
    SwipeRefreshLayout ptr_fresh_id;

    HomeMessageAdapter mHomeMessageAdapter;
    private String mPos = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        toolbar_title.setText(R.string.message_titile);
        right_text.setTextSize(16);
        right_text.setTextColor(getResources().getColor(R.color.text_important));
        right_text.setOnClickListener(this);
        toolbar_back.setOnClickListener(this);

        ptr_fresh_id.setColorSchemeColors(ContextCompat.getColor(MessageListActivity.this, R.color.text_red));
        ptr_fresh_id.setOnRefreshListener(MessageListActivity.this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MessageListActivity.this);
        rcv_list_id.setLayoutManager(linearLayoutManager);
        mHomeMessageAdapter = new HomeMessageAdapter(MessageListActivity.this, MessageListActivity.this);
        mHomeMessageAdapter.setOnLoadMoreListener(MessageListActivity.this, rcv_list_id);
        rcv_list_id.setAdapter(mHomeMessageAdapter);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        loadingView.setVisibility(View.GONE);

        ptr_fresh_id.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_fresh_id.setRefreshing(true);
                mPos = "";
                getPresenter().getUserMsgList(Global.getUserInfoBean()
                        .getUser()
                        .getUserNo(), "21", "", "20", "ALL");
            }
        }, 50);
    }

    @Override
    public void onRefresh() {
        mPos = "";
        getPresenter().getUserMsgList(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", "", "20", "ALL");
    }

    @Override
    public void onLoadMore() {
        getPresenter().getUserMsgList(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", mPos, "20", "ALL");
    }

    @Override
    public void onMessageListData(MessageListBean messageListBean, boolean state) {
        if (StringUtils.isEmpty(mPos)) {
            ptr_fresh_id.setRefreshing(false);
        }

        if (state) {
            if (StringUtils.isEmpty(mPos)) {
                if (messageListBean.getResult().size() <= 0) {
                    //empty
                    ((ImageView) emptyView.findViewById(R.id.iv_empty_icom)).setImageResource(R.drawable.empty_message_icon);
                    showEmpty(false);
                    return;
                }
                mHomeMessageAdapter.resetData(messageListBean.getResult());
                if (messageListBean.getResult().size() <= MESSAGE_MIN_NUM) {
                    ptr_fresh_id.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (!isFinishing()) {
                                mHomeMessageAdapter.loadMoreEnd(true);
                            }
                        }
                    }, 200);
                } else {
                    mHomeMessageAdapter.loadMoreComplete();
                }
                showContent();
            } else {
                mHomeMessageAdapter.addData(messageListBean.getResult());
                if (messageListBean.getResult().size() < PAGE_NUM_PRE) {
                    mHomeMessageAdapter.loadMoreEnd(false);
                } else {
                    mHomeMessageAdapter.loadMoreComplete();
                }
            }

            mPos = messageListBean.getPos();
        } else {
            if (StringUtils.isEmpty(mPos)) {
                mHomeMessageAdapter.loadMoreComplete();
                //((ImageView) errorView.findViewById(R.id.iv_error_icom))
                //        .setImageResource(R.drawable.empty_message_icon);
                ((TextView) errorView.findViewById(R.id.tv_error_text)).setText("网络迷失了，请重新导航");
                showError(false);
            } else {
                ptr_fresh_id.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isFinishing()) {
                            mHomeMessageAdapter.loadMoreFail();
                        }
                    }
                }, 200);
            }
        }

        boolean hasRead = false;
        for (MessageListBean.ResultItem resultItem : mHomeMessageAdapter.getData()) {
            if (resultItem.getReadstatus().equals("0")) {
                hasRead = true;
                break;
            }
        }

        if (hasRead) {
            right_text.setVisibility(View.VISIBLE);
            right_text.setText("全部已读");
        } else {
            right_text.setText("");
            right_text.setVisibility(View.GONE);
        }
    }

    @Override
    public <T> void onItemClick(View view, T t) {
        MessageListBean.ResultItem resultItem = (MessageListBean.ResultItem) t;
        getPresenter().setMessageRead(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", resultItem.getMessageno());
        if (resultItem.getContenttype().equals("01")) {
            Intent intent = new Intent(MessageListActivity.this, MessageDetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("resultItem", resultItem);
            intent.putExtras(bundle);
            startActivity(intent);
        } else if (resultItem.getContenttype().equals("02")) {
            Intent intent = new Intent(MessageListActivity.this, SimpleWebViewActivity.class);
            intent.putExtra(WebViewTbsActivity.WEBVIEW_TITLE, "消息");
            intent.putExtra(WebViewTbsActivity.WEBVIEW_URL, resultItem.getContent());
            startActivity(intent);
        }
    }

    @Override
    public <T> void onReadMsgBtnClick(View view, T t) {
        MessageListBean.ResultItem resultItem = (MessageListBean.ResultItem) t;
        getPresenter().setMessageRead(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", resultItem.getMessageno());
    }

    @Override
    public <T> void onDeleteBtnCilck(View view, T t) {
        MessageListBean.ResultItem resultItem = (MessageListBean.ResultItem) t;
        getPresenter().setMessageDelet(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", resultItem.getMessageno());
    }

    @Override
    protected void onErrorViewClicked() {
        mPos = "";
        getPresenter().getUserMsgList(Global.getUserInfoBean()
                .getUser()
                .getUserNo(), "21", "", "20", "ALL");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_back:
                finish();
                break;

            case R.id.right_text:
                String messageNo = "";
                for (MessageListBean.ResultItem resultItem : mHomeMessageAdapter.getData()) {
                    if (resultItem.getReadstatus().equals("0")) {
                        resultItem.setReadstatus("1");
                        if (TextUtils.isEmpty(messageNo)) {
                            messageNo = resultItem.getMessageno();
                        } else {
                            messageNo = messageNo + "," + resultItem.getMessageno();
                        }
                    }
                }
                mHomeMessageAdapter.notifyDataSetChanged();
                getPresenter().setMessageRead(Global.getUserInfoBean()
                        .getUser()
                        .getUserNo(), "21", messageNo);
                right_text.setText("");
                right_text.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }
}
